// <copyright file="ColumnSorter.cs" company="Sinclair Community College">
// Copyright (c) Sinclair Community College. All rights reserved.
// </copyright>

namespace SinclairCC.AppVSuite.Search
{
    using System;
    using System.Windows.Forms;

    /// <summary>
    /// Defines a sorting comparer for a ListView control.
    /// </summary>
    public class ColumnSorter : System.Collections.IComparer
    {
        /// <summary>
        /// The index of the column by which the control is being sorted.
        /// </summary>
        private int currentColumn;

        /// <summary>
        /// Indicates the order in which the control's items are sorted.
        /// </summary>
        /// <remarks>
        /// Positive values sort the items in ascending order.
        /// </remarks>
        private int sortOrder;

        /// <summary>
        /// Initializes a new instance of the ColumnSorter class.
        /// </summary>
        public ColumnSorter()
        {
            this.currentColumn = 0;
            this.sortOrder = 1;
        }

        /// <summary>
        /// Initializes a new instance of the ColumnSorter class.
        /// </summary>
        /// <param name="columnIndex">
        /// The index of the column by which the items will be sorted.
        /// </param>
        public ColumnSorter(int columnIndex) : this()
        {
            this.currentColumn = columnIndex;
        }

        /// <summary>
        /// Gets or sets a value indicating the index of the column by
        /// which the control's items are sorted.
        /// </summary>
        public int CurrentColumn
        {
            get { return this.currentColumn; }
            set { this.currentColumn = value; }
        }

        /// <summary>
        /// Changes the sort order from ascending to descending or vice-versa.
        /// </summary>
        public void ToggleSortOrder()
        {
            // Change the sign of sortOrder, which will change the sorting order.
            this.sortOrder *= -1;
        }

        #region IComparer Members

        /// <summary>
        /// Compares two objects and returns a value indicating whether the first
        /// object is less than, equal to, or greater than the second object. 
        /// </summary>
        /// <param name="x">
        /// The first object to be compared.
        /// </param>
        /// <param name="y">
        /// The second object to be compared.
        /// </param>
        /// <returns>
        /// Returns zero if the items are equal.
        /// Returns a negative value if x is less than y.
        /// Returns a positive value if y is less than x.
        /// </returns>
        public int Compare(object x, object y)
        {
            ListViewItem rowA = x as ListViewItem;
            ListViewItem rowB = y as ListViewItem;

            string textA = rowA.SubItems[this.currentColumn].Text;
            string textB = rowB.SubItems[this.currentColumn].Text;

            return this.sortOrder * String.Compare(textA, textB, true);
        }

        #endregion
    }
}
